<template>
  <div class="app-container">
    <div class="container">
      <!--新增编辑弹出框-->
      <viewSlideshow ref="viewSlideshow" />
      <!--工具栏-->
      <div>
        <el-form ref="query" :inline="true" :model="query" size="small" class="demo-form-inline">
          <el-form-item label="轮播图：" prop="carouse_name">
            <el-input
              v-model="query.carouse_name"
              clearable
              placeholder=""
              style="width: 180px;"
              class="filter-item"
              @keyup.enter.native="toQuery"/>
          </el-form-item>
          <el-form-item >
            <el-button size="mini" type="success" icon="el-icon-search" @click="toQuery">查询</el-button>
            <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!--新增按钮-->
        <div class="operate-container">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            @click="addSlideshow">新增轮播图
          </el-button>
        </div>
      </div>
      <!--表格渲染-->
      <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
        <el-table-column label="序号" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="轮播图名称" prop="carouse_name" align="center" />
        <el-table-column label="创建日期" prop="create_time" align="center" />
        <el-table-column label="操作" align="center" width="170">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="slideshowDetails(scope.row)"/>
            <el-popover
              :ref="scope.row.carouse_id"
              placement="top"
              width="180">
              <p>确定删除本条数据吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[scope.row.carouse_id].doClose()">取消</el-button>
                <el-button :loading="delLoading" type="primary" size="mini" @click="delClick(scope.row.carouse_id)">确定</el-button>
              </div>
              <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page="page + 1"
        style="margin-top: 8px;"
        background
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange" />
    </div>
  </div>
</template>

<script>
import initData from '@/mixins/initData'
import viewSlideshow from './viewSlideshow'
import { delSlideshow } from '@/api/design/slideshow'
export default {
  components: { viewSlideshow },
  mixins: [initData],
  data() {
    return {
      delLoading: false
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    beforeInit() {
      this.url = 'api/slideshow-list'
      const sort = 'create_time,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      const carouse_name = query.carouse_name
      if (carouse_name && carouse_name !== '' && carouse_name !== null) { this.params['carouse_name'] = carouse_name }
      return true
    },
    resetQuery() {
      this.$refs['query'].resetFields()
      this.init()
    },
    // 添加轮播图
    addSlideshow() {
      const _this = this.$refs.viewSlideshow
      _this.dialog = true
      _this.isAdd = true
      // 默认添加一条轮播图记录
      _this.form = { carouseList: [{ url: '', type: 0, link_url: '', link_name: '' }] }
    },
    // 编辑轮播图
    slideshowDetails(row) {
      const _this = this.$refs.viewSlideshow
      _this.dialog = true
      _this.isAdd = false
      // 新建一个Object对象，避免数据错乱，更改了当前表格的数据
      console.log('row', row)
      _this.form = Object.assign({}, row)
    },
    // 删除轮播图
    delClick(carouse_id) {
      this.delLoading = true
      const data = { carouse_id: carouse_id }
      delSlideshow(data).then(res => {
        if (res.result) {
          this.$notify({
            title: res.msg,
            type: 'success',
            duration: 2500
          })
        } else {
          this.$notify({
            title: res.msg,
            type: 'error',
            duration: 2500
          })
        }
        this.delLoading = false
        this.$refs[carouse_id].doClose()
        this.init()
      }).catch(err => {
        this.delLoading = false
        this.$refs[carouse_id].doClose()
        console.log(err.response.data.message)
      })
    }
  }
}
</script>
